<script setup lang="ts">
import { ref, watch, onMounted } from "vue";
import { useSystemStoreWithOut } from "@/store/modules/system";
import { storeToRefs } from "pinia";
import { getDictList } from "@/api/dict";

const systemStore = useSystemStoreWithOut();
const { materialTypeList } = storeToRefs(systemStore);

const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  tableHeight: {
    type: Number,
    default: 0
  }
});

const tableData = ref([]);
const curRow = ref({});
const stockAbnormalTypeList = ref([]);

const getStockAbnormalTypeList = () => {
  getDictList({ dictCode: "STOCK_ABNORMAL_TYPE" }).then((res: any) => {
    stockAbnormalTypeList.value = res.data || [];
  });
};

onMounted(() => {
  getStockAbnormalTypeList();
});

watch(
  () => props.list,
  (newVal: any) => {
    console.log(newVal);
    tableData.value = newVal;
  },
  { deep: true }
);

const handleRowClick = (row: any) => {
  curRow.value = row;
};
</script>

<template>
  <div>
    <el-table
      class="tableBox"
      :data="tableData"
      style="width: 100%"
      :height="tableHeight"
      @row-click="handleRowClick"
    >
      <el-table-column type="index" label="序号" min-width="80px" />
      <el-table-column prop="materialCode" label="物料编码" min-width="128px" show-overflow-tooltip/>
      <el-table-column prop="materialName" label="物料名称" min-width="120px" show-overflow-tooltip/>
      <el-table-column label="物料类型" prop="materialType" min-width="104px" show-overflow-tooltip>
        <template #default="scope">
          <el-text>{{
            materialTypeList.find(
              val => val.dictLabelValue == scope.row.materialType
            )?.dictLabelName
          }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        prop="materialSpecs"
        label="物料规格"
        min-width="200px"
        show-overflow-tooltip
      />
      <el-table-column prop="unit" label="单位" min-width="80px" show-overflow-tooltip/>
      <el-table-column
        prop="stockAbnormalType"
        label="异动类型"
        min-width="112px"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          {{
            stockAbnormalTypeList.find(
              val => val.dictLabelValue == row.stockAbnormalType
            )?.dictLabelName
          }}
        </template>
      </el-table-column>
      <el-table-column prop="reduceWarehouseName" label="异动减少仓库" min-width="136px" show-overflow-tooltip/>
      <el-table-column prop="increaseWarehouseName" label="异动增加仓库" min-width="136px" show-overflow-tooltip/>
      <el-table-column prop="num" label="异动数量" min-width="104px" show-overflow-tooltip/>
    </el-table>
  </div>
</template>
<style lang="scss" scoped>
.tableBox {
  .el-input {
    --el-input-width: 100% !important;
  }
}
</style>
